package com.phkj.qdh.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.core.BasePopupView
import com.phkj.qdh.R
import com.phkj.qdh.ui.adapter.CommonSelectAdapter
import com.phkj.qdh.ui.adapter.CommonSelectBean
import com.phkj.qdh.utils.MyItemDecoration

@SuppressLint("ViewConstructor")
class CommonSelectDialog(
    context: Context,
    val data: ArrayList<CommonSelectBean>,
    val isSingle: Boolean = false
) :
    BasePopupView(context) {
    private val commonSelectAdapter by lazy { CommonSelectAdapter() }
    override fun getInnerLayoutId(): Int {
        return R.layout.dialog_select_common
    }

//    override fun getImplLayoutId(): Int {
//        return R.layout.dialog_select_man
//    }

    private var title: String = ""
    private var unSelectTips = "请选择"

    fun setTitle(s: String) {
        this.title = s
    }

    fun setUnSelectTips(s: String) {
        this.unSelectTips = s
    }

    private var lastIndex: Int? = null
    override fun onCreate() {
        super.onCreate()
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        val btn = findViewById<ShapeButton>(R.id.btn_confirm)
        val tvTitle = findViewById<TextView>(R.id.tv_title)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            adapter = commonSelectAdapter
            addItemDecoration(MyItemDecoration(width = 10))
        }
        tvTitle.text = title
        commonSelectAdapter.submitList(data)
        commonSelectAdapter.setOnItemClickListener { adapter, _, pos ->
            if (!isSingle) {
                adapter.getItem(pos)!!.isSelected = !adapter.getItem(pos)!!.isSelected
                adapter.notifyItemChanged(pos)
            } else {
                adapter.items.forEach{
                    it.isSelected = false
                }
                adapter.getItem(pos) !!.isSelected = true
                adapter.notifyItemRangeChanged(0, adapter.items.size)

               /* if (lastIndex == null) {
                    adapter.getItem(pos)!!.isSelected = true
                    adapter.notifyItemChanged(pos)
                    lastIndex = pos
                } else {
                    if (lastIndex == pos) {
                        adapter.getItem(pos)!!.isSelected = !adapter.getItem(pos)!!.isSelected
                        adapter.notifyItemChanged(pos)
                    } else {
                        adapter.items.forEachIndexed { index, commonSelectBean ->
                            if (index != pos) {
                                commonSelectBean.isSelected = false
                            } else {
                                commonSelectBean.isSelected = true
                            }
                        }
                          }

                    }*/




            }
        }

        btn.setOnClickListener {
            val data = commonSelectAdapter.items.filter { it.isSelected }
            if (data.isNotEmpty()) {
                val pos = ArrayList<Int>()
                commonSelectAdapter.items.forEachIndexed { index, commonSelectBean ->
                    if (commonSelectBean.isSelected) {
                        pos.add(index)
                    }
                }
                if (isSingle){
                    commonSelectSingleListener?.ok(pos[0])
                }else{
                    commonSelectMultipleListener?.ok(pos)
                }
                dismiss()
            } else {
                ToastUtils.show(unSelectTips)
            }

        }
    }

    private var commonSelectMultipleListener: CommonSelectMultipleListener? = null
    fun setMultipleResultListener(listener: CommonSelectMultipleListener) {
        this.commonSelectMultipleListener = listener
    }

    private var commonSelectSingleListener: CommonSelectSingleListener? = null
    fun setSingleResultListener(listener: CommonSelectSingleListener) {
        this.commonSelectSingleListener = listener
    }
}


interface CommonSelectMultipleListener {
    fun ok(list: List<Int>)
}

interface CommonSelectSingleListener {
    fun ok(pos: Int)
}